import java.util.*;
class MyQueue1 {
    Stack<Integer> stack1;
    Stack<Integer> stack2;

    public MyQueue1() {
        stack1 = new Stack<>();
        stack2 = new Stack<>();
    }

    public void push(int x) {
        stack1.push(x);
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }

    public int peek() {
        if(empty()){
            return -1;
        }
        if(stack2.isEmpty()){
            while(!stack1.isEmpty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.peek();
    }

    public boolean empty() {
        return stack1.isEmpty() && stack2.isEmpty();
    }
}
class MyStack {
    public Queue<Integer> qu1;
    public Queue<Integer> qu2;
    public MyStack() {
        qu1 = new LinkedList<>();
        qu2 = new LinkedList<>();
    }

    public void push(int x) {
        if(qu1 == null && qu2 == null){
            qu1.offer(x);
        }
        if(!qu1.isEmpty()){
            qu1.offer(x);
        }else{
            qu2.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;
        }
        if(!qu1.isEmpty()){
            int size = qu1.size();
            for(int i=0;i<size-1;i++){
                qu2.offer(qu1.poll());
            }
            int val = qu1.poll();
            return val;
        }else{
            int size = qu2.size();
            for(int i=0;i<size-1;i++){
                qu1.offer(qu2.poll());
            }
            int val = qu2.poll();
            return val;
        }
    }

    public int top() {
        if(empty()){
            return -1;
        }
        if(!qu1.isEmpty()){
            int size = qu1.size();
            int val=-1;
            for(int i=0;i<size;i++){
                val = qu1.poll();
                qu2.offer(val);
            }
            return val;
        }else{
            int size = qu2.size();
            int val = -1;
            for(int i=0;i<size;i++){
                val = qu2.poll();
                qu1.offer(val);
            }
            return val;
        }
    }

    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}

class MyCircularQueue {
    public int[] elem;
    public int froat;
    public int rear;
    public MyCircularQueue(int k) {
        elem = new int[k+1];
    }

    public boolean enQueue(int value) {
        if(isFull()){
            return false;
        }
        elem[rear] = value;
        rear = (rear+1)%elem.length;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        froat = (froat+1)%elem.length;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[froat];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        int index = (rear == 0)?elem.length-1:rear-1;
        return elem[index];
    }

    public boolean isEmpty() {
        return rear == froat;
    }

    public boolean isFull() {
        return (rear+1)%elem.length == froat;
    }
}
public class MyQueue {
    static class Node{
        int val;
        Node next;
        public Node(int val){
            this.val=val;
        }
    }
    public Node head;
    public Node last;
    public int usedSize;
    //入队
    public void offer(int val){
        Node node = new Node(val);
        if(head == null){
            head = node;
            last = node;
            return;
        }
        last.next = node;
        last = last.next;
        usedSize++;
    }

    //出队
    public int poll(){
        if(isEmpty()){
            System.out.println("队列空");
            return -1;
        }
        int ret = head.val;
        head = head.next;
        if(head == null){
            last =null;
        }
        this.usedSize--;
        return ret;
    }

    private boolean isEmpty(){
        if(head == null){
            return true;
        }
        return false;
    }
    public int peek() {
        if(isEmpty()){
            System.out.println("队列空");
            return -1;
        }
        return head.val;
    }


}
